<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");
class CompanyController extends Controller {


    public function _initialize()
    {
        if(!check_login()){
            $this->error('登录状态异常,请重新登录','/Admin/Login/login');
        }
    }

    public function company_desc()
    {
        $info   =   M("company")->where(true)->find();
        $this->assign("info",$info);
        $this->display();
    }
    public function company_link()
    {
        $info   =   M("company")->where(true)->find();
        $this->assign("info",$info);
        $this->display();
    }

    public function dataDo()
    {
        if($_POST){
            $data   =   $_POST;
            $file1   =   $_FILES['pic']; // 单张图片


            $dir='./Uploads/banner/';
            if(!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }
            if($file1['name'][0]){
                $getPic     =   upPic($file1,$dir);
                if($getPic['tag'] == '1'){
                    $this->error($getPic['msg'].'1');
                }else  {
                    $data['pic'] = $getPic;
                }
            }

            $info   =   M("company")->where(true)->find();
            if($info['id']){
                $res    =   M("company")->where(array("id"=>$info['id']))->save($data);
            }else{
                $res    =   M("company")->add($data);
            }
            if($res !== false)  $this->success("操作成功");
            else    $this->error("操作失败");
        }else{
            $this->error("异常操作");
        }
    }


    //右侧轮播
    public function adv()
    {
        $banner = M('banner')->where(array('type'=>'1'))->order("sort asc")->select();
//      倒序 desc  正序asc
        $this->assign('banner',$banner);
        $this->display();
    }


    //轮播图添加
    public function advAdd()
    {
        $data['url'] = I('post.url');
        $file = $_FILES['pic'];
        if(empty($file['name']))
            die("<script>alert('图片不能为空');history.back(-1);</script>");
        $dir='./Uploads/banner/';
        if(!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        $tag = upPic($file,$dir);
        if($tag['tag']  == '1') $this->error($tag['msg']);
        $data['pic']    =   $tag;
        $data['type']    =   '1';
        $data['name']    =   I('post.name');
        $data['sort']    =   I('post.sort');
        $res = M('banner')->add($data);
        if($res) $this->redirect('Admin/Index/adv');
        else $this->error("添加失败");
    }



}